<?php

namespace addons\csmitsm\library\xcore\xcore\utils;

use think\Config;

/**
 * 
 * 具体配置参照: https://doc.fastadmin.net/developer/80.html <BR>
 * 
 * 配置支持datasoure下拉 ,具体示例如下:<BR>
 * [
        'type' => 'string',
        'extend' => 'data-source="auth/admin/selectpage" data-field="nickname" class="form-control selectpage"',
   ],
 */
class XcConfigUtils
{
    public static function isDebug()
    {
        return config("addons_debug");
    }

    public static function isXcDebug(){
        return static::xpconfigdev("addons_debug");
    }

    public static function getAddonCode()
    {
        return static::xpconfig("addons_code");
    }

    public static function config($key)
    {
        $config = get_addon_config(static::getAddonCode());
        return isset($config[$key]) ? $config[$key] : null;
    }

    public static function fullconfig($key)
    {
        $configs = get_addon_fullconfig(static::getAddonCode());
        foreach ($configs as $config) {
            if ($config['name'] == $key) {
                return $config;
            }
        }
        return null;
    }

    /**
     * 获取插件config.php的字段的值
         configFieldvalue("xcflow_mobiletpl","visible");
     * @return string|array
     */
    public static function configFieldvalue($key, $keyfield)
    {
        $value = null;

        $config = static::fullconfig($key);
        $value = isset($config[$keyfield]) ? $config[$keyfield] : null;
        if ($value != null && $keyfield == 'visible') {
            $value = explode('=', $value);
        }
        return $value;
    }


    /**
     * 获取插件config.php的配置是否被开启(基于visible=Y则视为开启)
     *
     * @param [type] $key
     * @return boolean
     */
    public static function isConfigEnable($key)
    {
        $visible = static::configFieldvalue($key, 'visible');
        if (empty($visible)) {
            return true;
        }

        $visibleValue = static::config($visible[0]);

        if ($visible[1] == $visibleValue) {
            return true;
        } else {
            return false;
        }
    }

    public static function clangconfig($key)
    {
        $val = XcConfigUtils::config($key);
        $enval = XcConfigUtils::config('en_' . $key);
        return XcLangUtils::getCLangText($val, $enval);
    }

    public static function xpconfig($key)
    {
        $sr = XcIncludeFileUtils::include_php_addons_file("config/xpConfig.php");
        return isset($sr[$key]) ? $sr[$key] : null;
    }
    public static function xpconfigdev($key)
    {
        $sr = XcIncludeFileUtils::include_php_addons_file("config/xpConfigDev.php");
        return isset($sr[$key]) ? $sr[$key] : null;
    }

    public static function databaseconfig($key)
    {
        $value = Config::get('database.' . $key);
        return $value;
    }

    public static function systemconfig($key)
    {
        return config($key);
    }
}
